<template>
  <div style="padding: 20px; background-color:gainsboro;">
<!-- 功能区 -->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="add">新增</el-button>
    </div>
<!-- 搜索区 -->
    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="请输入姓名进行查询" style="width: 20%;" />
      <el-button style="margin-left: 5px" type="primary" @click="cha">查询</el-button>
    </div>

    <el-table :data="tableData" border stripe style="width: 100%" >
    <el-table-column prop="people_id" label="成员id" sortable/>
    <el-table-column prop="people_name" label="姓名" />
    <el-table-column prop="gender" label="性别" />
    <el-table-column prop="age" label="年龄" />
    <el-table-column prop="family_id" label="家族id" />
    <el-table-column prop="family_name" label="家族名称" />
    <el-table-column prop="p_id" label="配偶id" >
      <template  #default="scope">
        {{scope.row.p_id==0?"":scope.row.p_id }}
      </template>
    </el-table-column>
    <el-table-column prop="m_id" label="母亲id" >
      <template  #default="scope">
        {{scope.row.m_id==0?"":scope.row.m_id }}
      </template>
    </el-table-column>
    <el-table-column prop="f_id" label="父亲id" >
      <template  #default="scope">
        {{scope.row.f_id==0?"":scope.row.f_id }}
      </template>
    </el-table-column>
    <el-table-column prop="date_birth" label="出生日期" />
    <el-table-column label="头像" >
      <template #default="scope">
        <el-image style="width: 100px; height: 100px;"
        :src="scope.row.picture"
        :preview-src-list="[scope.row.picture]">
        </el-image>
      </template>
    </el-table-column>
 

    <el-table-column label="操作">
      <template #default="scope">
        <el-button size="mini" @click="handleEdit(scope.row)" type="primary">编辑</el-button>
          <el-popconfirm title="确认删除吗？" @confirm="handleDelete(scope.row)">
    <template #reference>
      <el-button size="mini" type="danger">删除</el-button>
    </template>
  </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>

  <div style="margin: 10px 0">
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[5,10,20]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 增加弹出框 -->
    <el-dialog v-model="dialogVisible" title="新增" width="30%">
    <el-form :model="form" label-width="120px">
      <el-form-item label="成员id">
      <el-input v-model="form.people_id" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="姓名">
      <el-input v-model="form.people_name" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="性别">
      <el-input v-model="form.gender" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="年龄">
      <el-input v-model="form.age" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="家族id">
      <el-input v-model="form.family_id" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="家族名称">
      <el-input v-model="form.family_name" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="配偶id">
      <el-input v-model="form.p_id" style="width: 80%;" />
    </el-form-item>
    <template v-if="form2.m_id !=0">
    <el-form-item label="母亲id" >
         <el-input v-model="form.m_id" style="width: 80%;" />
    </el-form-item>
  </template>
    <el-form-item label="父亲id">
      <el-input v-model="form.f_id" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="出生日期">
      <el-input v-model="form.date_birth" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="头像">
    <el-upload action="http://localhost:8081/files/upload" :on-success="filesUploadSuccess">
  <el-button type="primary">点击上传</el-button>
  </el-upload>
    </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </span>
    </template>
  </el-dialog>

  <!-- 修改弹出框 -->
  <el-dialog v-model="dialogVisible2" title="编辑" width="30%">
    <el-form :model="form2" label-width="120px">
      <el-form-item label="成员id">
      <el-input v-model="form2.people_id" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="姓名">
      <el-input v-model="form2.people_name" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="性别">
      <el-input v-model="form2.gender" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="年龄">
      <el-input v-model="form2.age" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="家族id">
      <el-input v-model="form2.family_id" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="家族名称">
      <el-input v-model="form2.family_name" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="配偶id">
      <el-input v-model="form2.p_id" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="母亲id">
      <el-input v-model="form2.m_id" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="父亲id">
      <el-input v-model="form2.f_id" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="出生日期">
      <el-input v-model="form2.date_birth" style="width: 80%;" />
    </el-form-item>
    <el-form-item label="头像">
    <el-upload action="http://localhost:8081/files/upload" :on-success="filesUploadSuccess">
  <el-button type="primary">点击上传</el-button>
  </el-upload>
    </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible2 = false">取消</el-button>
        <el-button type="primary" @click="save2">确定</el-button>
      </span>
    </template>
  </el-dialog>
  </div>
  </div>
</template>



<script>

import request from "@/utils/request";

export default {
  name: 'People',

  components: {
    
  },
  
  data(){
    return{
      form:{},
      form2:{},
      dialogVisible:false,
      dialogVisible2:false,
      search:'',
      currentPage:1,
      pageSize:10,
      total:0,
      tableData:[]
    }
  },

  created(){
    this.load();
  },

  methods:{

    filesUploadSuccess(res){
      console.log(res)
      this.form2.picture =res.data
      this.form.picture =res.data
    },

    load(){

      request.get("/people/list", {
          params:{
            currentPage: this.currentPage,
            pageSize: this.pageSize
          }
        }).then(res => {
          this.tableData=res.data
        })
         // 获取总记录数
         request.get('/people/total')
          .then(response => {
            debugger;
            this.total = response;
          })
        .catch(error => {
          console.error(error);
        });

      },

      cha(){
          request.get("/people/search", {
            params:{
              search: this.search
            }
          }).then(res => {
            console.log(res)
            this.tableData=res.data
          })
        },

    add(){

      this.dialogVisible=true
      this.form={}

    },
    
    //新增
    save(){
      
        request.post("/people",this.form).then(res => {
          console.log(res)
        })
      
        this.dialogVisible = false
        this.load()

      },

    //更新
    save2(){

      request.put("/people",this.form2).then(res => {
          console.log(res)
        })

        this.dialogVisible2 = false
        this.load()
    },

      handleEdit(row){

      this.form2 = JSON.parse(JSON.stringify(row))
      this.dialogVisible2 = true

    },    

    //删除
    handleDelete(row){

      this.form2 = JSON.parse(JSON.stringify(row))

      request.put("/people/del",this.form2).then(res => {
        console.log(res)

        })

        this.load()
    },

    handleSizeChange(size){
      this.pageSize = size;
      this.load();
    },

    handleCurrentChange(page){
      this.currentPage = page;
      this.load()
    },
    },
}

</script>